package com.hjq.demo.app.vb

import androidx.viewbinding.ViewBinding
import com.hjq.base.BaseVBFragment
import com.hjq.demo.action.ToastAction
import com.hjq.http.listener.OnHttpListener
import com.hjq.demo.http.model.HttpData
import com.hjq.http.exception.TokenException
import okhttp3.Call
import java.lang.Exception

/**
 * @author he
 */
abstract class AppVbFragment<A : AppVbActivity<*>, T : ViewBinding> : BaseVBFragment<A, T>(), ToastAction,
    OnHttpListener<Any?> {

    /**
     * 当前加载对话框是否在显示中
     */
    open fun isShowDialog(): Boolean {
        val activity: A = getAttachActivity() ?: return false
        return activity.isShowDialog()
    }

    /**
     * 显示加载对话框
     */
    open fun showDialog() {
        val activity = getAttachActivity()
        activity?.showDialog()
    }

    /**
     * 隐藏加载对话框
     */
    open fun hideDialog() {
        val activity = getAttachActivity()
        activity?.hideDialog()
    }

    /**
     * [OnHttpListener]
     */
    override fun onStart(call: Call) {
        showDialog()
    }

    override fun onSucceed(result: Any?) {
        if (result is HttpData<*>) {
            toast(result.getMessage())
        }
    }

    override fun onFail(e: Exception) {
        toast(e.message)
    }

    override fun onEnd(call: Call) {
        hideDialog()
    }
}